﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace browser4gp.core.Helpers
{

    /// <summary>
    /// Helper per la deserializzazione dei dati in json
    /// </summary>
    public class JsonDeserializerHelper : JsonConverter
    {


        private readonly string BRUSH_TYPE = typeof(Brush).FullName;

        private readonly string WINDOW_STATE_TYPE = typeof(WindowState).FullName;

        private readonly string FONT_FAMILY_TYPE = typeof(FontFamily).FullName;

        private readonly string MARGIN_TYPE = typeof(Thickness).FullName;

        /// <summary>
        /// Indica se si può convertire il tipo di oggetto
        /// </summary>
        /// <param name="objectType">Oggetto da convertire</param>
        /// <returns>True se si può convertire</returns>
        public override bool CanConvert(Type objectType)
        {
            string fullName = objectType.FullName;
            return fullName == BRUSH_TYPE ||
                   fullName == WINDOW_STATE_TYPE ||
                   fullName == FONT_FAMILY_TYPE ||
                   fullName == MARGIN_TYPE;
        }

        /// <summary>
        /// Legge i dati 
        /// </summary>
        /// <param name="reader">Sorgente</param>
        /// <param name="objectType">Tipo oggetto</param>
        /// <param name="existingValue">Valore proprietà</param>
        /// <param name="serializer">Serializzatore</param>
        /// <returns>Dato deserializzato</returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            string fullName = objectType.FullName;
            if (fullName == BRUSH_TYPE)
            {
                return new BrushConverter().ConvertFromString(reader.Value as string) as Brush;
            }
            else if (fullName == FONT_FAMILY_TYPE)
            {
                return new FontFamilyConverter().ConvertFromString(reader.Value as string) as FontFamily;
            }
            else if (fullName == MARGIN_TYPE)
            {
                Thickness result = new Thickness(5);
                string value = reader.Value as string;
                if (!string.IsNullOrEmpty(value))
                {
                    string[] values = value.Split(',');
                    if (values.Count() == 1)
                    {
                        double doubleValue = 0;
                        double.TryParse(values[0], out doubleValue);
                        if (doubleValue > 0)
                        {
                            result = new Thickness(doubleValue);
                        }
                    }
                    else if (values.Count() == 4)
                    {
                        double left;
                        double top;
                        double right;
                        double bottom;
                        double.TryParse(values[0], out left);
                        double.TryParse(values[1], out top);
                        double.TryParse(values[2], out right);
                        double.TryParse(values[3], out bottom);
                        result = new Thickness(left, top, right, bottom);
                    }
                }
                return result;
            }
            else if (fullName == WINDOW_STATE_TYPE)
            {
                WindowState result = WindowState.Normal;
                string value = reader.Value as string;
                if (!string.IsNullOrEmpty(value))
                {
                    if (value.Equals("Minimized"))
                    {
                        result = WindowState.Minimized;
                    }
                    else if (value.Equals("Maximized", StringComparison.InvariantCultureIgnoreCase))
                    {
                        result = WindowState.Maximized;
                    }
                }
                return result;
            }
            throw new NotSupportedException(string.Format("Can't deserialize the object type {0} from json", fullName));
        }

        /// <summary>
        /// Scrive il dato in json
        /// </summary>
        /// <param name="writer">Dove scrivere</param>
        /// <param name="value">Valore da scrivere</param>
        /// <param name="serializer">Serializzatore da usare</param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            serializer.Serialize(writer, value);
        }
    }
}
